// 部分省市区数据
const placeArray = ['事业单位：630min 洞察“事”面', '国省考：21小时刷题网课专项', '选调：2023选调生暑期思路进阶班', '教师招聘：22年考情复盘&教基专项诠析 ', '卫生招聘：2022山东医疗-独当“医”面', '三支一扶：精选公基300新题讲练', '考研：管综初数&逻辑-技巧狂讲带练', '教师资格证：教师资格笔试-考点诠析', '专升本：英语夏令营', '农信社：农信社简历-网申指导', '银行秋招：2023银行校招网申简历修改', '国企校招：2023国企备考通行宝', '光明电力：2022光明电力笔试备考', '烟草局：2022山东烟草局充电站', '社区工作者：社区公基1000+题刷题'];

//获取应用实例
var app = getApp()
Page({
  data: {
    awardList: [], //奖品数组
    indexSelect: 0, //被选中的奖品index
    isRunning: false, //是否正在抽奖
    imageAward: [
      '../../../images/hz.png',
      '../../../images/hz.png',
      '../../../images/hz.png',
      '../../../images/hz.png',
      '../../../images/hz.png',
      '../../../images/hz.png',
      '../../../images/hz.png',
      '../../../images/hz.png',
    ], //奖品图片数组
    imageAwarda: [
      '../../../images/hz.png',
      '../../../images/hz.png',
      '../../../images/hz.png',
      '../../../images/hz.png',
      '../../../images/hz.png',
      '../../../images/hz.png',
      '../../../images/hz.png',
      '../../../images/hz.png',
    ], //奖品图片数组
    jp: '',
    jiang: false,
    inputShow: false,
    boxShow: false,
    placeArray: placeArray,
    djk: !1,
    array: ['请选择您的备考项目','硕士研究生考试','专升本考试','公务员','事业单位','教师招聘','医疗卫生','选调生','三支一扶','教师资格证','农信社','银行秋招','国企校招','光明电力','烟草局','社区工作者'],
    index: 0
  },
  // 展示弹框
  getbox: function () {
    this.setData({
      boxShow: true
    })
  },
  // 隐藏弹框
  hidebox: function () {
    this.setData({
      boxShow: false
    })
  },
  // 确认按钮
  confirm: function (e) {
    const _ = this;
    _.setData({
      boxShow: false,
      inputShow: true,
      address: _.data.province
    })
    console.log(_.data.address)
  },
  changeProvince2: function (e) {
    const val = e.detail.value
    this.setData({
      pIndex: val,
      province: placeArray[val]
    })
  },

  onLoad: function (options) {
    var _ = this;

    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/choujiang/getjlinfo?actid=50509&callback=?', //接收url
      data: {
        sstimes: Date.parse(new Date()),
        limits: 200
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson.lists)
        _.setData({
          zjlists: theJson.lists
        })
      }
    })

    _.setData({
      msgList: [{
          url: "url",
          title: "公告：多地首套房贷利率上浮 热点城市渐迎零折扣时代"
        },
        {
          url: "url",
          title: "公告：悦如公寓三周年生日趴邀你免费吃喝欢唱"
        },
        {
          url: "url",
          title: "公告：你想和一群有志青年一起过生日嘛？"
        }
      ]
    });
    if (options.area) {
      wx.setStorageSync('area', options.area)
      wx.setStorageSync('agent', options.agent)
      wx.setStorageSync('page', options.page)
    }

    //奖品item设置
    var awardList = [];
    //间距
    var topAward = 25;
    var leftAward = 30;
    for (var j = 0; j < 8; j++) {
      if (j == 0) {
        topAward = 15;
        leftAward = 15;
      } else if (j < 3) {
        topAward = topAward;
        //180是宽.15是间距.下同
        leftAward = leftAward + 168 + 12;
      } else if (j < 5) {
        leftAward = leftAward;
        //186是高,15是间距,下同
        topAward = topAward + 170 + 15;
      } else if (j < 7) {
        leftAward = leftAward - 168 - 12;
        topAward = topAward;
      } else if (j < 8) {
        leftAward = leftAward;
        topAward = topAward - 170 - 15;
      }
      var imageAward = this.data.imageAward[j];
      awardList.push({
        topAward: topAward,
        leftAward: leftAward,
        imageAward: imageAward
      });
    }
    this.setData({
      awardList: awardList
    })
  },
  submit() {
    var _ = this;
    var phone = wx.getStorageSync('kxjphone')

    if (phone) {
      wx.request({ //发送请求
        url: 'https://zg99.offcn.com/index/choujiang/getcjusers?actid=50509&callback=?', //接收url
        data: {
          phone: phone,
          sstimes: Date.parse(new Date()),
          limits: 100
        }, //接收参数
        success(res) {
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          }
          console.log(theJson)
          if (theJson.status == 2) {
            _.startGame()
          } else if (theJson.lists.length < 2) {
            let kxjShare = wx.getStorageSync('kxjShare')
            if (kxjShare) {
              _.startGame()
            } else {
              wx.showToast({
                title: '请先转发页面！',
                icon: 'error'
              })
              _.setData({
                isRunning: false,
              })
            }
          } else {
            wx.showModal({
              title: '次数不足',
              content: '您已经抽过两次奖了！',
              showCancel: false, //去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  _.setData({
                    isRunning: false,
                  })
                  _.xx()
                }
              }
            })
          }
        }
      })

    } else {
      wx.showToast({
        title: '请先登录',
        duration: 2000,
        icon: 'none'
      })
      wx.navigateTo({
        url: '../login/login',
      })
    }
  },

  //开始抽奖
  startGame: function () {
    if (this.data.isRunning) return
    this.setData({
      isRunning: true
    })
    var _ = this;
    var indexSelect = 0
    var i = 0;
    var timer = setInterval(function () {
      indexSelect++;
      var random = Math.random() * 100; //转盘
      // console.log(random)
      i += random;
      if (i > 1000) {
        //去除循环
        clearInterval(timer)
        //获奖提示
        var phone = wx.getStorageSync('kxjphone')
        wx.request({ //发送请求
          url: 'https://zg99.offcn.com/index/choujiang/getcjusers?actid=50509&callback=?', //接收url
          data: {
            phone: phone,
            sstimes: Date.parse(new Date()),
            limits: 100
          }, //接收参数
          success(res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace("(", "");
            jsonStr = jsonStr.replace(")", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
              var theJson = JSON.parse(jsonStr);
            }
            console.log(theJson)
            if (theJson.status == 2) {
              wx.request({ //发送请求
                url: 'https://zg99.offcn.com/index/choujiang/lottery?actid=50509&callback=?', //接收url
                data: {
                  phone: phone,
                  sstimes: Date.parse(new Date()),
                  isagree: true
                }, //接收参数
                success(res) {
                  var jsonStr = res.data;
                  jsonStr = jsonStr.replace("(", "");
                  jsonStr = jsonStr.replace(")", "");
                  if (typeof jsonStr != 'object') {
                    jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                    var theJson = JSON.parse(jsonStr);
                  }
                  console.log(theJson)
                  if (theJson.prizeid == 5) {
                    wx.setStorageSync('swdt', '1')
                  }
                  if (theJson.prizeid == 2) {
                    wx.setStorageSync('djk', '1')
                  }
                  if (theJson.prizeid == 1 || theJson.prizeid == 6) {
                    _.setData({
                      wxh: 1
                    })
                  }
                  _.setData({
                    jp: theJson.prizename,
                    jiang: true,
                    isRunning: false,
                    gz: !1,
                    ewm: !1,
                    zj: 1,
                    xx: !1,
                    jpxx: !1
                  })
                }
              })
            } else if (theJson.lists.length < 2) {
              let kxjShare = wx.getStorageSync('kxjShare')
              if (kxjShare) {
                wx.request({ //发送请求
                  url: 'https://zg99.offcn.com/index/choujiang/lottery?actid=50509&callback=?', //接收url
                  data: {
                    phone: phone,
                    sstimes: Date.parse(new Date()),
                    isagree: true
                  }, //接收参数
                  success(res) {
                    var jsonStr = res.data;
                    jsonStr = jsonStr.replace("(", "");
                    jsonStr = jsonStr.replace(")", "");
                    if (typeof jsonStr != 'object') {
                      jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                      var theJson = JSON.parse(jsonStr);
                    }
                    console.log(theJson)
                    if (theJson.prizeid == 5) {
                      wx.setStorageSync('swdt', '1')
                    }
                    if (theJson.prizeid == 2) {
                      wx.setStorageSync('djk', '1')
                    }
                    if (theJson.prizeid == 1 || theJson.prizeid == 6) {
                      _.setData({
                        wxh: 1
                      })
                    }
                    _.setData({
                      jp: theJson.prizename,
                      jiang: true,
                      isRunning: false,
                      gz: !1,
                      ewm: !1,
                      zj: 1,
                      xx: !1,
                      jpxx: !1
                    })
                  }
                })
              } else {
                wx.showToast({
                  title: '请先转发页面！',
                  icon: 'error'
                })
                _.setData({
                  isRunning: false,
                })
              }
            } else {
              wx.showModal({
                title: '次数不足',
                content: '您已经抽过两次奖了！',
                showCancel: false, //去掉取消按钮
                success: function (res) {
                  if (res.confirm) {
                    _.setData({
                      isRunning: false,
                    })
                    _.xx()
                  }
                }
              })
            }
          }
        })
      }
      indexSelect = indexSelect % 8;
      _.setData({
        indexSelect: indexSelect
      })
    }, (200 + i))
  },
  nameInput: function (e) {
    this.setData({
      name: e.detail.value
    })
    console.log(this.data.name)
  },
  schoolInput: function (e) {
    this.setData({
      school: e.detail.value
    })
    console.log(this.data.school)
  },
  addressInput: function (e) {
    this.setData({
      address: e.detail.value
    })
    console.log(this.data.address)
  },
  telInput: function (e) {
    this.setData({
      tel: e.detail.value
    })
    console.log(this.data.tel)
  },
  jpxx() {
    const _ = this;
    _.setData({
      jpxx: 1,
      gz: !1,
      ewm: !1,
      zj: !1,
      xx: !1
    })
  },
  ewm() {
    const _ = this;
    _.setData({
      gz: !1,
      ewm: 1,
      zj: !1,
      xx: !1,
      jpxx: !1
    })
  },
  close() {
    const _ = this;
    _.setData({
      gz: !1,
      jpxx: !1,
      swdt: !1,
      ewm: !1,
      zj: !1,
      sr: !1,
      xx: !1
    })
  },
  copywx() {
    const _ = this;
    const ma = 'sdxdbk'
    wx.setClipboardData({
      data: ma,
      success(res) {
        wx.getClipboardData({
          success(res) {
            wx.showToast({
              title: '微信号已复制！',
              duration: 5000
            })
          }
        })
      }
    })
  },
  gz() {
    const _ = this;
    _.setData({
      gz: 1,
      jpxx: !1,
      zj: !1,
      xx: !1
    })
  },
  xx() {
    const _ = this;
    let phone = wx.getStorageSync('kxjphone')
    let djk = wx.getStorageSync('djk')
    if (djk) {
      _.setData({
        djk: 1
      })
    }

    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/choujiang/getcjusers?actid=50509&callback=?', //接收url
      data: {
        phone: phone,
        sstimes: Date.parse(new Date()),
        isagree: true
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson.lists)
        if (theJson.lists.length == 2) {
          if (theJson.lists[0].prizeid == 2 || theJson.lists[1].prizeid == 2) {
            _.setData({
              djk: 1
            })
          }
        } else {
          if (theJson.lists[0].prizeid == 2) {
            _.setData({
              djk: 1
            })
          }
        }

        _.setData({
          jplist: theJson.lists
        })
      }
    })
    _.setData({
      gz: !1,
      zj: !1,
      xx: 1
    })
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  qdxx() {
    const _ = this;
    let phone = wx.getStorageSync('kxjphone')
    let djk = wx.getStorageSync('djk')
    console.log(_.data.addres)
    if (djk) {
      if (_.data.address == undefined) {
        wx.showToast({
          title: '请选择低价课！',
          icon: 'none'
        })
      } else {
        if(_.data.name == undefined){
          wx.showToast({
            title: '请填写姓名！',
            icon: 'none'
          })
          return false
        }
        if(_.data.tel == undefined){
          wx.showToast({
            title: '请填写领奖电话！',
            icon: 'none'
          })
          return false
        }
        if(_.data.index == 0){
          wx.showToast({
            title: '请选择您的备考项目!',
            icon: 'none'
          })
          return false
        }
        wx.request({ //发送请求
          url: 'https://zg99.offcn.com/index/choujiang/updateuser?actid=50509&callback=?', //接收url
          data: {
            phone: phone,
            sstimes: Date.parse(new Date()),
            isagree: true,
            name: _.data.name,
            school: _.data.array[_.data.index],
            tel: _.data.tel,
            address: _.data.address
          }, //接收参数
          success(res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace("(", "");
            jsonStr = jsonStr.replace(")", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
              var theJson = JSON.parse(jsonStr);
            }
            console.log(theJson)
            let swdt = wx.getStorageSync('swdt')
            if (swdt == 1) {
              _.setData({
                gz: !1,
                zj: !1,
                xx: !1,
                swdt: 1
              })
            } else {
              _.setData({
                gz: !1,
                zj: !1,
                xx: !1
              })
              wx.showToast({
                title: '提交成功！',
              })
            }

          }
        })
      }
    } else {
      if(_.data.name == undefined){
        wx.showToast({
          title: '请填写姓名！',
          icon: 'none'
        })
        return false
      }
      if(_.data.tel == undefined){
        wx.showToast({
          title: '请填写领奖电话！',
          icon: 'none'
        })
        return false
      }
      if(_.data.index == 0){
        wx.showToast({
          title: '请选择您的备考项目!',
          icon: 'none'
        })
        return false
      }
      wx.request({ //发送请求
        url: 'https://zg99.offcn.com/index/choujiang/updateuser?actid=50509&callback=?', //接收url
        data: {
          phone: phone,
          sstimes: Date.parse(new Date()),
          isagree: true,
          name: _.data.name,
          school: _.data.array[_.data.index],
          tel: _.data.tel,
          address:'无'
        }, //接收参数
        success(res) {
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          }
          console.log(theJson)
          let swdt = wx.getStorageSync('swdt')
          if (swdt == 1) {
            _.setData({
              gz: !1,
              zj: !1,
              xx: !1,
              swdt: 1
            })
          } else {
            _.setData({
              gz: !1,
              zj: !1,
              xx: !1
            })
            wx.showToast({
              title: '提交成功！',
            })
          }

        }
      })
    }

  },
  swdt() {
    wx.showLoading({
      title: '下载中...',
    })
    wx.downloadFile({
      // 示例 url，并非真实存在
      url: 'https://sd.offcn.com/zg/wximg/2022/xd/kxjcj/swdt.pdf',
      success: function (res) {
        console.log(res)
        const filePath = res.tempFilePath;
        const fs = wx.getFileSystemManager(); //拿到小程序整个文件系统
        wx.openDocument({
          filePath: filePath,
          success: function (res) {
            console.log('打开文档成功')
            wx.hideLoading({
              success: (res) => {},
            })
          },
          fail(res) {
            console.log(res)
          }
        })


      },
      fail(res) {
        console.log(res)
      }
    })
  },
  onShareAppMessage() {
    const _ = this;
    setTimeout(function () {
      _.setData({
        sr: 1,
        gz: !1,
        jpxx: !1,
        ewm: !1,
        zj: !1,
        xx: !1
      })
    }, 3000)
    wx.setStorageSync('kxjShare', '1')
    let area = wx.getStorageSync('area')
    let agent = wx.getStorageSync('agent')
    let page = wx.getStorageSync('page')
    return {
      title: '新学期，‘抽’出好运气',
      path: '/pages/kxjcj/login/login?area=' + area + '&agent=' + agent + '&page=' + page // 路径，传递参数到指定页面。
    }
  },
})